//
//  MyWorkListVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/4/6.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "MyWorkListVC.h"
#import "searchCollectionCell.h"
#import "MyWorkVC.h"

@interface MyWorkListVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UIView *btn_view;
@property (strong, nonatomic) IBOutlet UIButton *left_btn;
@property (strong, nonatomic) IBOutlet UIButton *center_btn;
@property (strong, nonatomic) IBOutlet UIButton *right_btn;

@property (strong, nonatomic) IBOutlet UICollectionView *collection_view;
@end

@implementation MyWorkListVC
{
    NSInteger page;
    RequestServices *service;
    NSMutableArray *listArr;
    NSMutableDictionary *typeDic;//类型
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的作品";
    [self.backKuang setHidden:YES];
    self.left_btn.titleLabel.font = self.center_btn.titleLabel.font = self.right_btn.titleLabel.font = AutoLabFont2;
    [self.collection_view registerNib:[UINib nibWithNibName:@"searchCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"searchCollectionCell"];
    UIImageView *img = [[UIImageView alloc]init];
    img.image = [UIImage imageNamed:@"mine_btnView"];
    [self.btn_view addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.btn_view.mas_centerX);
        make.centerY.equalTo(self.btn_view.mas_centerY);
        make.width.equalTo(self.btn_view.mas_width);
        make.height.equalTo(self.btn_view.mas_height);
    }];
    
    self.collection_view.delegate = self;
    self.collection_view.dataSource = self;
    
    listArr = [NSMutableArray array];
    //类型默认1赛事   0活动  3学习中心
    typeDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1",@"type", nil];
    

    service = [[RequestServices alloc]initWithDelegate:self];
    [self headerRefresh];
    __weak __typeof(self)weakSelf = self;
    //左划
    self.collection_view.mj_trailer =[MJRefreshTrailer trailerWithRefreshingBlock:^{
        [weakSelf footerRefresh];
    }];
    self.collection_view.mj_trailer.hidden = YES;
}

- (void)headerRefresh
{
    service.view = self.view;
    page = 1;
    [service myWorkList:page type:typeDic[@"type"] tag:8000];
}

- (void)footerRefresh
{
    page++;
    [service myWorkList:page type:typeDic[@"type"] tag:8000];
}

#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self HideLoading];
    if (tag == 8000 || tag == 8001) {
        [self.collection_view.mj_trailer endRefreshing];
        NSMutableArray *tempArr = [NSMutableArray arrayWithArray:obj[@"list"]];
        if (tag == 8000) {
                listArr = [AliefuModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
        }else {
            [listArr addObjectsFromArray:[AliefuModel mj_objectArrayWithKeyValuesArray:tempArr]];
        }
        
        [self.collection_view reloadData];
    }
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self ShowNewMsg:error.domain];
    [self.collection_view.mj_trailer endRefreshing];
    
}



-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return listArr.count;
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(collectionView.frame.size.width/3,collectionView.frame.size.height * 0.9);
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    searchCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"searchCollectionCell" forIndexPath:indexPath];
    cell.model = listArr[indexPath.row];
    [cell.status_btn setHidden:YES];
    [cell.clock_img setHidden:YES];
    [cell.look_btn setTitle:@"查看详情" forState:UIControlStateNormal];
    cell.look_btn.tag = indexPath.row + 1000;
    [cell.look_btn addTarget:self action:@selector(pushInforVC:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

/*
 * 赛事  type = 1
 */
- (IBAction)chooseLeft:(id)sender {
    [self.left_btn setBackgroundImage:[UIImage imageNamed:@"aliefu_titleBtn2_2"] forState:UIControlStateNormal];
    [self.center_btn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [self.right_btn setBackgroundImage:[UIImage imageNamed:@"aliefu_titlebtn1_1"] forState:UIControlStateNormal];
    [typeDic setValue:@"1" forKey:@"type"];
    [self headerRefresh];
}

/*
 * 活动 type = 0
 */
- (IBAction)chooseCenter:(id)sender {
    [self.left_btn setBackgroundImage:[UIImage imageNamed:@"aliefu_titleBtn1"] forState:UIControlStateNormal];
    [self.center_btn setBackgroundImage:[UIImage imageNamed:@"mine_art"] forState:UIControlStateNormal];
    [self.right_btn setBackgroundImage:[UIImage imageNamed:@"aliefu_titlebtn1_1"] forState:UIControlStateNormal];
    [typeDic setValue:@"0" forKey:@"type"];
    [self headerRefresh];
}

/*
 * 学习中心 type=3
 */
- (IBAction)choose_right:(id)sender {
    [self.left_btn setBackgroundImage:[UIImage imageNamed:@"aliefu_titleBtn1"] forState:UIControlStateNormal];
    [self.center_btn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [self.right_btn setBackgroundImage:[UIImage imageNamed:@"aliefu_titlebtn2"] forState:UIControlStateNormal];
    [typeDic setValue:@"3" forKey:@"type"];
    [self headerRefresh];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    AliefuModel *model = listArr[indexPath.row];
    MyWorkVC *vc = [[MyWorkVC alloc]init];
    NSDictionary *dic = [model mj_keyValues];
    vc.valueDic = dic;
    [self PushToViewController:vc];
}



/*
 * 查看详情
 */
-(void)pushInforVC:(UIButton *)sender{
    NSInteger tag = sender.tag - 1000;
    AliefuModel *model = listArr[tag];
    MyWorkVC *vc = [[MyWorkVC alloc]init];
    NSDictionary *dic = [model mj_keyValues];
    vc.valueDic = dic;
    [self PushToViewController:vc];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
